//执行  npm run config 生成配置文件.

let fs = require('fs');

const encoding = 'utf-8';

let timestamp = Date.now();

const buildData = fs.readFileSync('package.json', {encoding: encoding});

const buildJson = JSON.parse(buildData);

const config = buildJson.config;

const env = process.env.DLP_ENV || 'dev';

const conf = config[env];

console.info('当前环境:%s', env)

//===================================================

const $version = buildJson.version;

const $baseUrl = conf.baseUrl;

const $debug = conf.debug || false;


//==============================================================

console.info('$baseUrl=>', $baseUrl);

//================================================

let now = new Date(timestamp);

let dataStr = now.getFullYear() + ''
    + ((now.getMonth() + 1) < 10 ? ('0' + (now.getMonth() + 1)) : (now.getMonth() + 1)) + ''
    + ((now.getDate()) < 10 ? ('0' + (now.getDate())) : (now.getDate()));

let evn = `/**
 * 该文件自动生成,请不要手动修改!
 * 执行 npm run config:xxxx 进生成. xxxx:[local,dev,prod]
 */
export class Config {
    static readonly version: string = '${$version}';
    static readonly packageTime: string = '${dataStr}';
    static readonly timestamp: number = ${timestamp};
    static readonly baseUrl = '${$baseUrl}';
    static readonly debug = ${$debug};
    static readonly env = '${env}';
}
`;
fs.writeFileSync('src/Config.ts', evn, 'utf-8');
